#!/bin/bash

echo "========================================"
echo "    THQMCP Server 启动脚本"
echo "========================================"

# 检查Java环境
if ! command -v java &> /dev/null; then
    echo "❌ 错误：未找到Java环境，请先安装Java 17+"
    exit 1
fi

JAVA_VERSION=$(java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}')
echo "✅ Java版本: $JAVA_VERSION"

# 设置环境变量（如果需要）
if [ -z "$OPENAI_API_KEY" ]; then
    echo "⚠️  警告：未设置OPENAI_API_KEY环境变量，AI功能可能无法正常工作"
    echo "   请使用: export OPENAI_API_KEY=your-api-key"
fi

echo ""
echo "🔧 编译项目..."
./mvnw clean compile

if [ $? -eq 0 ]; then
    echo "✅ 编译成功"
    echo ""
    echo "🚀 启动服务器..."
    echo "服务器将在 http://localhost:8080 启动"
    echo ""
    echo "可用的API端点："
    echo "  - GET  /mcp/info       - 获取服务器信息"
    echo "  - GET  /mcp/tools      - 获取工具列表"
    echo "  - POST /mcp/tools/call - 调用工具"
    echo "  - WebSocket: ws://localhost:8080/mcp/ws"
    echo ""
    echo "按 Ctrl+C 停止服务器"
    echo "========================================"
    
    ./mvnw spring-boot:run
else
    echo "❌ 编译失败，请检查错误信息"
    exit 1
fi 